package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.util.TransactionUtil;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: AdminProfileServiceImpl
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/6/2 15:27
 * @Version: V1.0
 */
public class AdminProfileServiceImpl implements AdminProfileService {
    @Override
    public BaseRespVo updatePassword(Map read) {
        String oldPassword = (String) read.get("oldPassword");
        String newPassword = (String) read.get("newPassword");
        String newPassword1 = (String) read.get("newPassword2");

        TransactionUtil.startTransaction();
        MarketAdminMapper mapper = TransactionUtil.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.createCriteria().andPasswordEqualTo(oldPassword);
        List<MarketAdmin> marketAdmins = mapper.selectByExample(marketAdminExample);
        if(marketAdmins.size() == 0){
            return BaseRespVo.fail(605,"账号密码不对");
        }
        if(newPassword.equals(newPassword1)){
            marketAdmins.get(0).setPassword(newPassword);
            int i = mapper.updateByPrimaryKey(marketAdmins.get(0));
        }
        TransactionUtil.commit();
        return BaseRespVo.ok();

    }
}
